package com.maserb.monitor.service.schoolStudent.impl;

import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.SysCode;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolStudent.SchoolTimeTableEntity;
import com.maserb.monitor.repository.schoolStudent.SchoolTimeTableRepository;
import com.maserb.monitor.service.author_manage.UserService;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.jwEducation.JwOrgService;
import com.maserb.monitor.service.schoolStudent.SchoolTimeTableService;
import com.maserb.monitor.service.sysCode.SysCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class SchoolTimeTableServiceImpl extends JpaSpecificationServiceImpl<SchoolTimeTableRepository,
        SchoolTimeTableRepository, SchoolTimeTableEntity, String>
        implements SchoolTimeTableService {

    @Autowired
    private SchoolTimeTableRepository schoolTimeTableRepository;

    @Autowired
    SysCodeService codeService;

    @Autowired
    JwOrgService orgService;

    @Autowired
    UserService userService;

    @Override
    public List<SchoolTimeTableEntity> findByClazz(String clazzId) {
        return schoolTimeTableRepository.findAllByClazzId(clazzId);
    }

    @Override
    public Page<SchoolTimeTableEntity> findAll(Pageable var1,String userId, String timeType, String day, String courseId, String clazzId) {
        List<JwOrgEntity>clazzList = new ArrayList<JwOrgEntity>();
        String schoolId = "";
        if(!StringUtils.isEmptyOrWhitespace(userId)){
            UserEntity user = userService.findOne(userId);
            schoolId = user.getOrgId();
            JwOrgEntity org = orgService.findOne(schoolId);
            if(org.getTreeLevel() < 1){
                schoolId = "";
            }
            if(!StringUtils.isEmptyOrWhitespace(clazzId)){
                JwOrgEntity jwOrgEntity = orgService.findOne(clazzId);
                if(jwOrgEntity.getTreeLevel()==1){
                    schoolId = clazzId;
                    clazzId="";
                }
            }
            clazzList = orgService.findByUser(user);
        }
        return this.findAll(getSpecification(clazzList,timeType, day, courseId, clazzId,schoolId),var1);
    }

    @Override
    public List<SchoolTimeTableEntity> findAll(String userId,String timeType, String day, String courseId, String clazzId) {
        List<JwOrgEntity>clazzList = new ArrayList<JwOrgEntity>();
        String schoolId = "";
        if(!StringUtils.isEmptyOrWhitespace(userId)){
            UserEntity user = userService.findOne(userId);
            schoolId = user.getOrgId();
            JwOrgEntity org = orgService.findOne(schoolId);
            if(org.getTreeLevel() < 1){
                schoolId = "";
            }
            if(!StringUtils.isEmptyOrWhitespace(clazzId)){
                JwOrgEntity jwOrgEntity = orgService.findOne(clazzId);
                if(jwOrgEntity.getTreeLevel()==1){
                    schoolId = clazzId;
                    clazzId="";
                }
            }
            clazzList = orgService.findByUser(user);
        }
        return new ArrayList(this.findAll(getSpecification(clazzList,timeType, day, courseId, clazzId,schoolId),new PageRequest(0,1000)).getContent());
    }

    private Specification<SchoolTimeTableEntity> getSpecification(final List<JwOrgEntity>clazzList,final String timeType, final String day,final String courseId,final String clazzId,final String schoolId){
        return new Specification<SchoolTimeTableEntity>()  {
            @Override
            public Predicate toPredicate(Root<SchoolTimeTableEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathTimeType = root.get("timeType");
                Path<String> pathDay = root.get("day");
                Path<SysCode> pathCourse = root.get("course");
                Path<JwOrgEntity> pathClazz = root.get("clazz");
                Path<String> pathSchool = root.get("schoolId");


                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(timeType)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathTimeType,timeType)));
                }
                if (!StringUtils.isEmptyOrWhitespace(day)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathDay,day)));
                }
                if (!StringUtils.isEmptyOrWhitespace(schoolId)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathSchool,schoolId)));
                }
                if (!StringUtils.isEmptyOrWhitespace(courseId)) {
                    SysCode code = codeService.findOne(courseId);
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathCourse, code)));
                }
                if (!StringUtils.isEmptyOrWhitespace(clazzId)) {
                    JwOrgEntity orgEntity = orgService.findOne(clazzId);
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathClazz, orgEntity)));
                }
                Expression<JwOrgEntity> exp = root.get("clazz");
                if(null!=clazzList && clazzList.size()>0){
                    list.add(exp.in(clazzList));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }
}